package love.contact;

import java.io.File;

import wwk.common.util.FileUtil;
import wwk.common.util.SysUtil;
import wwk.common.widget.NavigationBar;
import love.contact.engine.Engine;
import love.contact.engine.TheApplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;

/**
 * @author wangweike
 * @create_date 2011-12-27����10:17:46
 * @update_date 2011-12-27����10:17:46
 */

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class SplashActivity extends Activity {
    boolean isFirstIn = false;

    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    // �ӳ�1��
    private static final long SPLASH_DELAY_MILLIS = 1000;

    private static final String SHAREDPREFERENCES_NAME = "first_pref";

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case GO_HOME:
                goHome();
                break;
            case GO_GUIDE:
                //goGuide();
                break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
		SysUtil.setFullScreen(this);
		
		ImageView imageView=new ImageView(this);
		imageView.setImageResource(R.drawable.splash);
		setContentView(imageView);
		
		Intent intent=getIntent();
		Uri uri=(Uri)intent.getData();
		if(uri!=null)
        {
			//http://www.lephone.net/thread-63-1-1.html
			
        	String path=uri.getPath(); 
        	Log.d("getPath",path);
        	
        	TheApplication theApp=(TheApplication)getApplication();
		    theApp.engine.pendingFilePathName=path;
        }

        init();
    }

    private void init() 
    {
    	String sdCard=FileUtil.getSdCardDir();
        if(sdCard!=null)
        {
	        String path=sdCard+File.separator+SysUtil.getPackageName(this)+File.separator;
	        	FileUtil.createPath(path);

    		if(!FileUtil.isFileExists(path+"qrcode.png"))
    		{
    			byte[] bytes=FileUtil.read(R.drawable.qrcode, this);
    			FileUtil.write(path+"qrcode.png", bytes, bytes.length, false);
    		}
        }
    	
        SharedPreferences preferences = getSharedPreferences(
                SHAREDPREFERENCES_NAME, MODE_PRIVATE);

        isFirstIn = preferences.getBoolean("isFirstIn", true);

        //if (!isFirstIn) 
        {
            mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
        } 
        //else 
        {
            //mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
        }
    }

    private void goHome() 
    {
        Intent intent = new Intent(SplashActivity.this, MainTabActivity.class);
        //Intent intent = new Intent(SplashActivity.this, ItemListActivity.class);
        SplashActivity.this.startActivity(intent);
        SplashActivity.this.finish();
    }

//    private void goGuide() 
//    {
//        Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
//        SplashActivity.this.startActivity(intent);
//        SplashActivity.this.finish();
//    }
}
